<?php

class Controller_Site_Cabinet extends Controller_Site_Profile 
{
	protected $consultant;
	
	public function preDispatch()
	{
		parent::preDispatch();
		//$this->_addJs("/javascript/cabinet.js");
		$this->consultant = ConsultantPeer::getInstance()->fetchRow("profile_id = " . $this->profile->getId());
		if (!$this->consultant) {
			$this->_redirect("/");
			return;
		}
		$peer = OrdersPeer::getInstance();
		$select = $peer->select();
		$select -> where('status = 0 AND consultant_id = '. $this->consultant->getId());
		$row = $peer->fetchAll($select);
		$this->view->count_new_order = count($row);
		$this->view->consultant_id = $this->consultant->getId();
		$this->view->new_order = ContentList::fromRowset($row);
//>коммент для отката
//               $ret = ConsultantService::getConsultantScheduleRight($this->consultant->getId(), true);
//             
//            //    $ret = ConsultantService::getConsultantScheduleRight($this->consultant->getId(), false);
//
//                $this->view->schedule_right = $ret[0];
//                $this->view->schedule_message = $ret[1];
 //<
              
	}
	
	/**
	 * форматирование времени
	 * @deprecated перенесено в метод базового сервиса
	 * 
	 * @param int $period
	 * @param boolean $text
	 */
	public function formatTimePeriod($period, $text = false){
		return $this->getConsultantService()->formatTimePeriod($period, $text);
	}

	public function ajaxNewordersAction(){
		$this->_ajaxOnly();
		$this->_disableView();
		$text = "";
		$prefix = "";
		$peer = OrdersPeer::getInstance();
		$select = $peer->select();
		$select -> where('status = 0 AND consultant_id = '. $this->consultant->getId());
		$row = $peer->fetchAll($select);
		$crow = ContentList::fromRowset($row);
		foreach($crow as $key => $value){
			switch ($value->getTypeId()) {
				case 1:
					$prefix = 'Звонок от пользователя ';
					break;
				case 2:
					$prefix = 'Чат с пользователем ';
					break;
				case 3:
					$prefix = 'Обратный звонок пользователю ';
					break;
				case 4:
					$prefix = 'Личная встреча с пользователем ';
					break;
				case 5:
					$prefix = 'Письмо от пользователя ';
					break;
				case 7:
					$prefix = 'Смс-чат с пользователем ';
					break;
			};
			$hide = ($key!='0') ? 'style="display:none"' : '';
			//$text = $text . '<a href="/cabinet/orders/index/order/status/dir/asc" ' . $hide . ' class="step' . $key . '">' . $prefix . $value->client->name . '</a>';
			
			if ($value->getTypeId() == 2) {
				$text = $text . '<a title="Открыть заказ" href="/cabinet/chat/talk/id/'.$value->item_id.'"' . $hide . 'class="step' . $key . '">' . $prefix . $value->client->name . '</a>';
			} elseif ($value->getTypeId() == 7) {
				$text = $text . '<a title="Открыть заказ" href="/cabinet/sms-chat/talk/id/'.$value->item_id.'"' . $hide . 'class="step' . $key . '">' . $prefix . $value->client->name . '</a>';
			} else {
				$text = $text . '<a title="Открыть заказ" onclick="return Orders.order('.  $value->id.')"  href="#"' . $hide . 'class="step' . $key . '">' . $prefix . $value->client->name . '</a>';
			}

		//	$text = $text . '<a title="Открыть заказ" onclick="return Orders.order('.  $value->id.')"  href="#"' . $hide . 'class="step' . $key . '">' . $prefix . $value->client->name . '</a>';
		};
		$this->_setJson(array('orders'=> $text, 'cnt'=>count($row)));
	}

}